package com.qu.waibao.wxapi;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.qu.waibao.AppConfig;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, AppConfig.WXAPPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.e("TAG","resp-----------");
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.e("TAG","resp-----------"+resp);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
                sendBroadcast(new Intent("com.qu.waibao.PAYMENTSUCCESS"));
            } else if (resp.errCode == -2){
                Toast.makeText(this, "支付已取消", Toast.LENGTH_SHORT).show();

            } else{
                Toast.makeText(this, "支付失败，请重试", Toast.LENGTH_SHORT).show();

            }
        } else {
            if (resp.errCode == -2) {
               Toast.makeText(this, "支付已取消", Toast.LENGTH_SHORT).show();

            } else{
                Toast.makeText(this, "支付失败，请重试", Toast.LENGTH_SHORT).show();

            }
        }
        finish();
    }
}